/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 *  The greatest achievement is selflessness. The greatest worth is self-mastery.
 *  The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 *  The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 *  The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 *  The greatest goodness is a peaceful mind. The greatest patience is humility.
 *  The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 *  The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 * 
 * @author Ashin Wimalajeewa (ash)
 */
package org.javaongems.gwk.client;

import java.util.Iterator;

import org.javaongems.std.client.DivPanel;

import com.google.gwt.user.client.ui.Composite;

public class CardDeck extends Composite {
	static public interface Listener {
		public boolean onBeforeCardOpenned(Card sender);
		public void onCardOpenned(Card sender);
		public boolean onBeforeCardClosed(Card sender);
		public void onCardClosed(Card sender);
	}
	
	static public class ListenerAdapter implements Listener {
		public boolean onBeforeCardClosed(Card sender) { return true; }
		public boolean onBeforeCardOpenned(Card sender) { return true; }
		public void onCardClosed(Card sender) {}
		public void onCardOpenned(Card sender) {}
	}
	
	private DivPanel container = new DivPanel();
	private Listener listener;

	public void setListener(Listener lsnr) {
		listener = lsnr;
	}

	public CardDeck() {
		initWidget(container);
		setStyleName("gwk-CardDeck");
	}
	
	public void add(Card card) {
		int count = container.getWidgetCount();
		container.add(card);
		if (count > 0) {
			Card prev = (Card) container.getWidget(count-1);
			prev.refresh();
		}
		card.setCardDeck(this);
		card.refresh();
	}
	
	public void remove(Card card) {
		int pos = container.getWidgetIndex(card);
		if (pos >= 0) {
			container.remove(pos);
			card.setCardDeck(null);
			if (pos == 0) {
				int count = container.getWidgetCount();
				if (count > 0) {
					Card newFirst = (Card) container.getWidget(0);
					newFirst.refresh();
				}
			}
		}
	}
	
	public Card get(int idx) {
		return (Card) container.getWidget(idx);
	}
	
	public Iterator iterator() {
		return container.iterator();
	}
	
	public int size() {
		return container.getWidgetCount();
	}
	
	public void clear() {
		int count = size();
		for (int i = 0; i < count; i++) {
			Card toRemove = get(0);
			remove(toRemove);
		}
	}
	
	public boolean isLastCard(Card card) {
		int count = container.getWidgetCount();
		if (count > 0) {
			Card currLast = (Card) container.getWidget(count-1);
			return card == currLast;
		}
		return false;
	}
	
	public boolean isFirstCard(Card card) {
		int count = container.getWidgetCount();
		if (count > 0) {
			Card currLast = (Card) container.getWidget(0);
			return card == currLast;
		}
		return false;
	}
	
	public Card getPreviousCard(Card card) {
		Card ret = null;
		int count = container.getWidgetCount();
		if (count > 0) {
			int pos = container.getWidgetIndex(card);
			if (pos > 0)
				ret = (Card) container.getWidget(pos-1);
		}
		return ret;
	}

	public Card getNextCard(Card card) {
		Card ret = null;
		int count = container.getWidgetCount();
		if (count > 1) {
			int pos = container.getWidgetIndex(card);
			if (pos < count-1)
				ret = (Card) container.getWidget(pos+1);
		}
		return ret;
	}
	
	protected boolean onBeforeCardOpenned(Card sender) {
		if (listener != null)
			return listener.onBeforeCardOpenned(sender);
		return true;
	}
	
	protected void onCardOpenned(Card sender) {
		if (listener != null)
			listener.onCardOpenned(sender);
	}

	protected boolean onBeforeCardClosed(Card sender) {
		if (listener != null)
			return listener.onBeforeCardClosed(sender);
		return true;
	}
	
	protected void onCardClosed(Card sender) {
		if (listener != null)
			listener.onCardClosed(sender);
	}
}
